package com.schoolcloud.social.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.social.entity.SocPostCategory;
import com.schoolcloud.social.query.CategoryQuery;
import com.schoolcloud.social.service.SocPostCategoryService;
import com.schoolcloud.social.mapper.SocPostCategoryMapper;
import com.schoolcloud.social.vo.SocPostCategoryTreeVO;
import com.schoolcolud.daomybatisplus.dto.MpPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【soc_post_category(帖子分类表)】的数据库操作Service实现
 * @createDate 2025-03-09 08:51:21
 */
@Service
public class SocPostCategoryServiceImpl extends ServiceImpl<SocPostCategoryMapper, SocPostCategory>
        implements SocPostCategoryService {

    @Override
    public List<SocPostCategoryTreeVO> getPostCategoryTree() {
//        筛选出一级分类
        List<SocPostCategory> collect = list().stream().filter(item -> item.getParentId() == 0).toList();
//        封装为vo类型
        List<SocPostCategoryTreeVO> parentVos = collect.stream().map(item -> {
            SocPostCategoryTreeVO vo = new SocPostCategoryTreeVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        List<SocPostCategoryTreeVO> vos = list().stream().map(item -> {
            SocPostCategoryTreeVO vo = new SocPostCategoryTreeVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).collect(Collectors.toList());
        return convertToTree(parentVos, vos);
    }

    @Override
    public List<SocPostCategoryTreeVO> getCategoryParentTreeList() {
//        排除第三季分类
        List<SocPostCategoryTreeVO> collect = lambdaQuery().ne(SocPostCategory::getLevel, 3).list().stream().map(item -> {
            SocPostCategoryTreeVO vo = new SocPostCategoryTreeVO();
            BeanUtils.copyProperties(item, vo);
            return vo;
        }).toList();
        List<SocPostCategoryTreeVO> parents = collect.stream().filter(item -> item.getParentId() == 0).collect(Collectors.toList());
        SocPostCategoryTreeVO vo = new SocPostCategoryTreeVO();
        vo.setCategoryId(0);
        vo.setName("顶级分类");
        vo.setChildren(convertToTree(parents, collect));
        ArrayList<SocPostCategoryTreeVO> list = new ArrayList<>();
        list.add(vo);
        return list;
    }

    @Override
    public PageDTO<SocPostCategory> getPostCategoryPage(CategoryQuery query) {
        String keyword = query.getKeyword();
        Integer parentId = query.getParentId();
        Integer level = query.getLevel();
        Page<SocPostCategory> page = lambdaQuery()
                .like(StrUtil.isNotBlank( keyword), SocPostCategory::getName, keyword)
                .eq(parentId != null && parentId!=-1, SocPostCategory::getParentId, parentId)
                .eq(level != null && level!=-1, SocPostCategory::getLevel, level)
                .page(query.toMpPage());
        return MpPageUtil.of(page, SocPostCategory.class);
    }

    public List<SocPostCategoryTreeVO> convertToTree(List<SocPostCategoryTreeVO> parentList, List<SocPostCategoryTreeVO> list) {
        if (parentList==null || parentList.size()==0){
            return null;
        }
//        遍历父级结构
        parentList.forEach(parentItem -> {
            List<SocPostCategoryTreeVO> collect = list.stream().filter(childItem -> Objects.equals(childItem.getParentId(), parentItem.getCategoryId())).collect(Collectors.toList());
            if (collect.size() > 0) {
                //            递归继续遍历子集合
                convertToTree(collect, list);
            }
            parentItem.setChildren(collect);
        });
        return parentList;
    }
}




